package com.leejxx.health.controller;

import com.leejxx.health.common.MessageConst;
import com.leejxx.health.entity.Result;
import com.leejxx.health.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.security.core.userdetails.User;
/**
 * @Author LeeJx
 * @ClassName UserController
 * @Description TODO
 * @date 2020/12/29 16:42
 * @Version 1.0
 */
@RestController
@Slf4j
@RequestMapping("/user")
public class UserController {

    @DubboReference
    private UserService userService;

//    @RequestMapping("/login")
//    public Result login(@RequestBody LoginParam loginParam, HttpServletRequest request) {
//        log.info("[用户-登录]data:{}", loginParam.getUsername());
//        User user = userService.findByUsername(loginParam.getUsername());
//        if (null == user || !user.getPassword().equals(loginParam.getPassword())) {
//            return new Result(false, MessageConst.LOGIN_FAIL);
//        } else {
//            request.getSession().setAttribute("loginUser", user);
//            return new Result(true, MessageConst.LOGIN_SUCCESS, user);
//        }
//    }

    @RequestMapping("/loginSuccess")
    public Result loginSuccess() {
        return new Result(true, MessageConst.LOGIN_SUCCESS);
    }

    @RequestMapping("/loginFail")
    public Result loginFail() {
        return new Result(false, "登录失败");
    }

    @RequestMapping("/getUsername")
    public Result getUsername() {
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        if (null != principal && principal instanceof User) {
            User user = (User) principal;
            return new Result(true, MessageConst.GET_USERNAME_SUCCESS, user.getUsername());
        }
        return new Result(false, MessageConst.GET_USERNAME_FAIL);
    }
}
